#include "graphClass.h"

graphClass::graphClass(const wstring filePathIN,COLORREF colorIN,HWND hWndIN)   
{
	hWnd = hWndIN;
	valid = false;        
	started = false;
	filePath = filePathIN;
	//for now
	int cutIndex = filePath.find_last_of('\\');
	graphName = filePath.substr(cutIndex+1); 
	color = colorIN;
	values;
	maxValue = 0;
	minValue = 0;
	yScale = 1;
	yOffset = 0;
	visible = true;
	input.open(filePath.c_str());                                                
	if (!input)
	{
		cerr << "Couldn't open file to read";
		input.close();
		return;
	}
	else
	{
		//!check for errors (file emty etc)
		input>>period;        //READ PERIOD FROM FILE HERE (first line in file is the period)
		valid = true;
		timer = timeSetEvent(period, 0,timerProc,(DWORD_PTR)this,TIME_PERIODIC);
		started = true;
	}
}
graphClass::graphClass(const graphClass &copy)    
{
	period = copy.period;
	filePath = copy.filePath;   
	graphName = copy.graphName;    
	color = copy.color;
	values;
	started = false;
	valid = copy.valid;
	yScale = copy.yScale;
	yOffset = copy.yOffset;
	maxValue = 0;
	minValue = 0;
	visible = copy.visible;
	if(valid)
	{
		input.open(filePath.c_str());
		input>>period;
		timer = timeSetEvent(period, 0,timerProc,(DWORD_PTR)this,TIME_PERIODIC);
		started = true;
	}
	hWnd=copy.hWnd;
}

graphClass::~graphClass()
{
	if (started)
	{
		timeKillEvent(timer);
	}
	input.close();
}

void graphClass::start()
{
	timer = timeSetEvent(period, 0,timerProc,(DWORD_PTR)this,TIME_PERIODIC);
	started = true;
}

void graphClass::stop()
{
	timeKillEvent(timer);
	started = false;
}

void graphClass::sendMes()
{
	SendMessage(hWnd,(WM_USER+100),NULL,NULL);
}

int graphClass::getPeriod()
{
	return period;
}

void graphClass::loadValue()
{
	double valueIN;
	if(!input.eof())
	{
		input >> valueIN;    //until eof
		values.push_back(valueIN);
		if (valueIN > maxValue)
			maxValue = valueIN;
		if (valueIN < minValue)
			minValue = valueIN;
		sendMes();
	}
	else
	{
		this->stop();
	}	
	return;
}


int graphClass::getLength()
{
	return values.size();
}

void graphClass::setYScale(double yScaleIN)
{
	this->yScale = yScaleIN;
}

void graphClass::setYOffset(int yOffsetIN)
{
	this->yOffset = yOffsetIN;
}

double graphClass::getYScale()
{
	return this->yScale;
}

int graphClass::getYOffset()
{
	return this->yOffset;
}

double graphClass::getValue(int i)
{
	return this->values[i];
}

void graphClass::show()
{
	this->visible = true;
}

void graphClass::hide()
{
	this->visible = false;
}

bool graphClass::isVisible()
{
	return this->visible;
}

bool graphClass::isStarted()
{
	return this->started;
}

COLORREF graphClass::getColor()
{
	return this->color;
}

void graphClass::setColor(COLORREF colorIN)
{
	this->color = colorIN;
}

HWND graphClass::getHCtrl()
{
	return this->hCtrl;
}

void graphClass::setHCtrl(HWND hWnd)
{
	this->hCtrl=hWnd;
}

wstring graphClass::getGraphName()
{
	return this->graphName;
}

void graphClass::setGraphName(wstring graphNameIN)
{
	this->graphName = graphNameIN;
}

wstring graphClass::getFilePath()
{
	return this->filePath;
}

double graphClass::getMaxValue()
{
	return this->maxValue;
}

double graphClass::getMinValue()
{
	return this->minValue;
}

void CALLBACK timerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)    
{
	((graphClass*)dwUser)->loadValue();        
}
